class Solution
{
public:
    vector<int> colorTheArray(int n, vector<vector<int>> &queries)
    {
        vector<int> answer;
        vector<int> colors(n, 0);
        int count = 0;
        for (auto &query : queries)
        {
            int pos = query[0];
            int color = query[1];
            if (color == colors[pos])
            {
                answer.push_back(count);
                continue;
            }

            if (pos > 0)
            {
                if (colors[pos - 1] == color)
                {
                    ++count;
                }
                else if (colors[pos - 1] == colors[pos] && colors[pos] != 0)
                {
                    --count;
                }
            }

            if (pos < n - 1)
            {
                if (colors[pos + 1] == color)
                {
                    ++count;
                }
                else if (colors[pos + 1] == colors[pos] && colors[pos] != 0)
                {
                    --count;
                }
            }

            colors[pos] = color;
            answer.push_back(count);
        }
        return answer;
    }
};